//
//  GuidePageViewController.m
//  CreditHub
//
//  Created by chenglang on 2024/9/11.
//

#import "OrderViewController.h"
#import "OrderMenuCollectionViewCell.h"
#import "OrderListViewController.h"

@interface OrderViewController ()<UIPageViewControllerDataSource, UIPageViewControllerDelegate,UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, strong) NSArray *arryViewControllers;
@property (strong, nonatomic) UIPageViewController *pageViewController;
@property (nonatomic, strong) NSArray *arrayTabbar;

@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleLabel.text = @"Order for goods";
    
    self.arrayTabbar = @[@"All",@"Apply",@"Repayment",@"Finished"];
    self.selectIndex = 0;
    self.view.backgroundColor = UIColor.whiteColor;
    
    UIImageView *imageBg = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"home_dakawei_header"]];
    [self.view addSubview:imageBg];
    [imageBg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_equalTo(0);
    }];
    
    
    // 创建引导页视图控制器数组
    OrderListViewController *view1 = [[OrderListViewController alloc] init];
    view1.type = 4;
    OrderListViewController *view2 = [[OrderListViewController alloc] init];
    view2.type = 7;
    OrderListViewController *view3 = [[OrderListViewController alloc] init];
    view3.type = 6;
    OrderListViewController *view4 = [[OrderListViewController alloc] init];
    view4.type = 5;

    
    self.arryViewControllers = @[view1,view2,view3,view4];
    //    UIPageViewControllerTransitionStyleScroll
    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    self.pageViewController.dataSource = self;
    self.pageViewController.delegate = self;
    // 设置初始视图控制器
    [self.pageViewController setViewControllers:@[view1] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    
    
    // 将PageViewController的视图添加到主视图中
    CGRect rect =self.view.bounds;
    rect.origin.y = 50+SYS_TopHeight;
    rect.size.height = rect.size.height - SYS_TopHeight-50;
    self.pageViewController.view.frame = rect;
    self.pageViewController.view.cornerRadius = 30;
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];
    
    for (UIView *view in self.pageViewController.view.subviews) {
        if([view isKindOfClass:[UIScrollView class]]){
            UIScrollView *scrollview = (UIScrollView *)view;
            scrollview.scrollEnabled = NO;
        }
    }
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, SYS_TopHeight, SCREEN_WIDTH, 50) collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = UIColor.clearColor;
    [self.view addSubview:self.collectionView];
    
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"OrderMenuCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"OrderMenuCollectionViewCell"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationOrder:) name:NotificationOrderKey object:nil];
}

- (void)notificationOrder:(NSNotification *)nofication{
    NSInteger index = [nofication.object[@"index"] integerValue];
    if(index<self.arryViewControllers.count){
        
        self.selectIndex = index;
        [self.collectionView reloadData];
        [self.pageViewController setViewControllers:@[self.arryViewControllers[index]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    }
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrayTabbar.count; // 这里可以根据实际情况返回单元格的数量
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    OrderMenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OrderMenuCollectionViewCell" forIndexPath:indexPath];
    cell.lblTitle.text = self.arrayTabbar[indexPath.row];
    cell.lblLine.hidden = YES;
    cell.lblTitle.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    if(self.selectIndex == indexPath.row){
        cell.lblLine.hidden = NO;
        cell.lblTitle.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    }
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(SCREEN_WIDTH/4.0, 50);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    self.selectIndex = indexPath.row;
    [self.collectionView reloadData];
    [self.pageViewController setViewControllers:@[self.arryViewControllers[indexPath.row]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}


#pragma mark - UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = [self.arryViewControllers indexOfObject:viewController];
    if (index == 0) {
        return nil;
    }
    return self.arryViewControllers[index - 1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = [self.arryViewControllers indexOfObject:viewController];
    if (index == self.arryViewControllers.count - 1) {
        return nil;
    }
    return self.arryViewControllers[index + 1];
}



- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    if (completed) {
        UIViewController *currentViewController = self.arryViewControllers[[self.arryViewControllers indexOfObject:pageViewController.viewControllers[0]]];
        //        NSUInteger index = [self.arryViewControllers indexOfObject:currentViewController];
        //        self.pageControl.currentPage = index;
    }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
